<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_unitkerja extends CI_Controller {
	function C_unitkerja(){
		parent::__construct();
		if($this->auth->is_logged_in() == false){
			redirect('c=c_login','refresh');
		}
		$this->load->model('m_unitkerja', '', TRUE);
	}
	
	public function index(){
		$this->m_public_function->clear_cache();
		$this->load->view('main/v_unitkerja');
	}
	
	function get_list(){
		//$test = isset($_POST['test']) ? @$_POST['test'] : @$_GET['test'];
		//$this->firephp->log($test);
		$result=$this->m_unitkerja->get_list();
		echo $result;
	}
	
	function get_jab_byunitkerja(){
		$kodeunit = isset($_POST['kodeunit']) ? @$_POST['kodeunit'] : @$_GET['kodeunit'];
		$result=$this->m_unitkerja->get_jab_byunitkerja($kodeunit);
		echo $result;
	}
	
	function get_sub_byunitkerja(){
		$kodeunit = isset($_POST['kodeunit']) ? @$_POST['kodeunit'] : @$_GET['kodeunit'];
		$depth = isset($_POST['depth']) ? @$_POST['depth'] : @$_GET['depth'];
		$result=$this->m_unitkerja->get_sub_byunitkerja($kodeunit, $depth);
		echo $result;
	}
	
	//event handler action
	function get_action(){
		$task = isset($_POST['task']) ? @$_POST['task'] : @$_GET['task'];
		switch($task){
			case "CREATE":
				$this->create();
				break;
			case "UPDATE":
				$this->update();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	function create(){
		$kodeunit=trim(@$_POST["kodeunit"]);
		$kodeunit=htmlentities($kodeunit,ENT_QUOTES);
		$p_kodeunit=trim(@$_POST["p_kodeunit"]);
		$p_kodeunit=htmlentities($p_kodeunit,ENT_QUOTES);
		$namaunit=trim(@$_POST["namaunit"]);
		$namaunit=htmlentities($namaunit,ENT_QUOTES);
		$leaf=trim(@$_POST["leaf"]);
		$leaf=htmlentities($leaf,ENT_QUOTES);
		//data detail jabatan
		$rowsjab=@$_POST["rows"];
		
		/*$arr = array(0 => array(id=>1,name=>"cat 1"),
				1 => array(id=>2,name=>"cat 2"),
				2 => array(id=>3,name=>"cat 1")
		);
		
		$stack = array();
		$equal_value = 0;
		for($i=0; $i<sizeof($arr); $i++){
			if(in_array($arr[$i]['name'], $stack)){
				$this->firephp->log($arr[$i]['name']);
				$equal_value;
			}else {
				array_push($stack, $arr[$i]['name']);
			}
		}
		$this->firephp->log($stack);*/
		
		$result=$this->m_unitkerja->create($kodeunit, $p_kodeunit, $namaunit, $leaf, $rowsjab);
		echo $result;
	}
	
	function update(){
		$kodeunit=trim(@$_POST["kodeunit"]);
		$kodeunit=htmlentities($kodeunit,ENT_QUOTES);
		$namaunit=trim(@$_POST["namaunit"]);
		$namaunit=htmlentities($namaunit,ENT_QUOTES);
		$leaf=trim(@$_POST["leaf"]);
		$leaf=htmlentities($leaf,ENT_QUOTES);
		//data detail jabatan
		$rowsjab=@$_POST["rows"];
		
		$result=$this->m_unitkerja->update($kodeunit, $namaunit, $leaf, $rowsjab);
		echo $result;
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */